/*******************************************************************************
 * Copyright (c) 2012, 2013 XML-Print Project.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     XML-Print Project - initial API and implementation
 ******************************************************************************/
package info.xmlprint.util;

import java.text.Collator;
import java.util.Locale;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

/**
 * @author
 * @since 2008.12.2
 */
public class TableColumnSortor {

	/**
	 * sortieren nach Ziffer
	 * 
	 * @param table
	 * 
	 * @param column
	 * 
	 */
	public void addNumberSorter(final Table table, final TableColumn column) {
		column.addListener(SWT.Selection, new Listener() {
			boolean isAscend = true; // Absteigend

			public void handleEvent(Event e) {
				int columnIndex = getColumnIndex(table, column);
				TableItem[] items = table.getItems();
				//
				for (int i = 1; i < items.length; i++) {
					String strvalue2 = items[i].getText(columnIndex);
					if (strvalue2.equalsIgnoreCase("")) {
						//
						break;
					}
					for (int j = 0; j < i; j++) {
						String strvalue1 = items[j].getText(columnIndex);
						// String to float
						float numbervalue1 = Float.valueOf(strvalue1);
						float numbervalue2 = Float.valueOf(strvalue2);
						boolean isLessThan = false;
						if (numbervalue2 < numbervalue1) {
							isLessThan = true;
						}
						if ((isAscend && isLessThan)
								|| (!isAscend && !isLessThan)) {
							String[] values = getTableItemText(table, items[i]);
							Object obj = items[i].getData();
							items[i].dispose();
							TableItem item = new TableItem(table, SWT.NONE, j);
							item.setText(values);
							item.setData(obj);
							items = table.getItems();
							break;
						}
					}
				}
				table.setSortColumn(column);
				table.setSortDirection((isAscend ? SWT.UP : SWT.DOWN));
				isAscend = !isAscend;
			}
		});
	}

	/**
	 * String Sortieren
	 * 
	 * @param table
	 * 
	 * @param column
	 * 
	 */
	public void addStringSorter(final Table table, final TableColumn column) {
		column.addListener(SWT.Selection, new Listener() {
			boolean isAscend = true; // Absteigend
			Collator comparator = Collator.getInstance(Locale.getDefault());

			public void handleEvent(Event e) {
				int columnIndex = getColumnIndex(table, column);
				String selectedItemName = "";
				if (table.getSelection().length > 0)
					selectedItemName = table.getSelection()[0].getText();

				Image img = table.getItem(0).getImage();
				TableItem[] items = table.getItems();
				// sort
				for (int i = 1; i < items.length; i++) {
					String str2value = items[i].getText(columnIndex);
					if (str2value.equalsIgnoreCase("")) {
						// wenn blank, stop
						break;
					}
					for (int j = 0; j < i; j++) {
						String str1value = items[j].getText(columnIndex);
						boolean isLessThan = comparator.compare(str2value,
								str1value) < 0;

						if ((isAscend && isLessThan)
								|| (!isAscend && !isLessThan)) {
							String[] values = getTableItemText(table, items[i]);
							Object obj = items[i].getData();
							items[i].dispose();
							TableItem item = new TableItem(table, SWT.NONE, j);
							item.setText(values);
							item.setData(obj);
							item.setImage(img);
							items = table.getItems();

							break;
						}
					}
				}
				table.setSortColumn(column);
				table.setSortDirection((isAscend ? SWT.UP : SWT.DOWN));
				isAscend = !isAscend;
				for (TableItem ti : table.getItems()) {
					if (ti.getText().equals(selectedItemName)) {
						table.setSelection(ti);

						break;
					}
				}

			}
		});
	}

	/**
	 * 
	 * 
	 * @param table
	 * 
	 * @param column
	 * 
	 */
	public int getColumnIndex(Table table, TableColumn column) {
		TableColumn[] columns = table.getColumns();
		for (int i = 0; i < columns.length; i++) {
			if (columns[i].equals(column))
				return i;
		}
		return -1;
	}

	/**
	 * 
	 * 
	 * @param table
	 * 
	 * @param column
	 * 
	 */
	public String[] getTableItemText(Table table, TableItem item) {
		int count = table.getColumnCount();
		String[] strs = new String[count];
		for (int i = 0; i < count; i++) {
			strs[i] = item.getText(i);
		}
		return strs;
	}
}
